//
//  CRBaseViewController.m
//  NewAge
//
//  Created by dllo on 15/11/26.
//  Copyright © 2015年 ColorRevolution. All rights reserved.
//

#import "CRBaseViewController.h"

@interface CRBaseViewController ()

@end

@implementation CRBaseViewController

#pragma checkNetWorkStatus

// 网络判断
- (void)checkNetWorkWithblock:(void (^)(NetStatus status))block {
    
    AFNetworkReachabilityManager *netManager = [AFNetworkReachabilityManager sharedManager];
    [netManager startMonitoring];
    
    [netManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        NSLog(@"%ld", status);
        
        switch (status) {
            case -1:
            {
                [MBProgressHUDManager showTextHUDtoView:self.view string:@"网络请求失败, 请检查网络后重试" showPlace:Top height:15];
                block(Unknow);
                break;
            }
            case 0:
            {
                [MBProgressHUDManager showTextHUDtoView:self.view string:@"网络请求失败, 请检查网络后重试" showPlace:Top height:15];
                block(NotReachable);
                break;
            }
            case 1:
                block(WWAN);
                break;
            case 2:
                block(WiFi);
                break;
                
            default:
                break;
        }
    }];
    
    [netManager stopMonitoring];
}

// 网络判断并显示提示
- (void)checkNetWorkShowAlertWithblock:(void (^)(NetStatus status))block {
    
    
    AFNetworkReachabilityManager *netManager = [AFNetworkReachabilityManager sharedManager];
    [netManager startMonitoring];
    
    [netManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
            switch (status) {
                case -1:
                {
                    block(Unknow);
                    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"当前网络状态" message:@"未知" preferredStyle:UIAlertControllerStyleAlert];
                    UIAlertAction *confrimAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
                    [alert addAction:confrimAction];
                    
                    [self presentViewController:alert animated:YES completion:^{
                        
                    }];
                    break;
                }
                case 0:
                {
                    block(NotReachable);
                    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"当前网络状态" message:@"未连接" preferredStyle:UIAlertControllerStyleAlert];
                    UIAlertAction *confrimAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
                    [alert addAction:confrimAction];
                    
                    [self presentViewController:alert animated:YES completion:^{
                        
                    }];
                    break;
                }
                case 1:
                {
                    block(WWAN);
                    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"当前网络状态" message:@"移动网络" preferredStyle:UIAlertControllerStyleAlert];
                    UIAlertAction *confrimAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
                    [alert addAction:confrimAction];
                    
                    [self presentViewController:alert animated:YES completion:^{
                        
                    }];
                    break;
                }
                case 2:
                {
                    block(WiFi);
                    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"当前网络状态" message:@"wifi" preferredStyle:UIAlertControllerStyleAlert];
                    UIAlertAction *confrimAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
                    [alert addAction:confrimAction];
                    
                    [self presentViewController:alert animated:YES completion:^{
                        
                    }];
                    break;
                }
                    
                default:
                    break;
            }
        }
        else {
            switch (status) {
                case -1:
                {
                    block(Unknow);
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"当前网络状态" message:@"未知" delegate:nil cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
                    [alert show];
                    break;
                }
                case 0:
                {
                    block(NotReachable);
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"当前网络状态" message:@"未连接" delegate:nil cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
                    [alert show];
                    
                    break;
                }
                case 1:
                {
                    block(WWAN);
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"当前网络状态" message:@"移动网络" delegate:nil cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
                    [alert show];
                    
                    break;
                }
                case 2:
                {
                    block(WiFi);
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"当前网络状态" message:@"WLAN" delegate:nil cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
                    [alert show];
                    
                    break;
                }
                    
                default:
                    break;
            }
        }
        
    }];
    
    [netManager stopMonitoring];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
